<?php
class Clients
{
	const DBTable = "tbl_clients";

	public $Client_id;	public $Client_md5;	public $Title;	public $Logo;	public $Client_from;	public $Client_end;
	public $Is_client;	public $Status;	public $Is_deleted;	public $Createdate;	public $Updatedate;
	private $__mysql;

	public function __construct()
	{
		$this->Client_id = 0;		$this->Client_md5 = null;		$this->Title = null;		$this->Logo = null;		$this->Client_from = date("Y-m-d");		$this->Client_end = date("Y-m-d");		$this->Is_client = 1;
		$this->Status = 0;		$this->Is_deleted = 0;		$this->Createdate = date("Y-m-d H:i:s");		$this->Updatedate = date("Y-m-d H:i:s");
		$this->__mysql = new MYSQL;
	}

	public function Save()
	{
		$sqlStr = "insert into ".self::DBTable."
				(title, logo, client_from, client_end, is_client, status, createdate, updatedate)
				values
				(
				'".mysql_real_escape_string($this->Title)."', 				'".mysql_real_escape_string($this->Logo)."', 				'".$this->Client_from."', 				'".$this->Client_end."', 
				'".$this->Is_client."', 				'".$this->Status."', 				'".$this->Createdate."', 				'".$this->Updatedate."'
				)";
			$this->__mysql->Exec_query($sqlStr);
			$this->Client_id = $this->__mysql->Get_insert_id();
			
			//create an md5
			$this->Client_md5 = substr(md5($this->Client_id), 0, 10);
			$this->Quick_update("client_md5", $this->Client_md5);
			
			return $this->Client_id;
	}

	public function Update()
	{
		//deal with any assets
		$query = "select logo from ".self::DBTable." where client_id = '". mysql_real_escape_string($this->Client_id)."'";
		$recordset = $this->__mysql->Exec_query($query);
		$result = $this->__mysql->Get_data_assoc($recordset);

		if($this->Logo != "" || $this->Logo == "delete")
		{
			if($this->Logo == "delete")
			{
				$this->Logo = null;
				@unlink(ASSET_STORE_CLIENTS . $result['logo']);
			}

			$this->Quick_update("logo", $this->Logo);
		}
		//

		$sqlStr = "update ".self::DBTable." set
			title = '".mysql_real_escape_string($this->Title)."', 			logo = '".mysql_real_escape_string($this->Logo)."', 			client_from = '".$this->Client_from."', 			client_end = '".$this->Client_end."', 
			is_client = '".$this->Is_client."', 			status = '".$this->Status."', 			updatedate = '".$this->Updatedate."'
			where client_id = '".mysql_real_escape_string($this->Client_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Quick_update($column = null, $value = null)
	{
		$sqlStr = "update ".self::DBTable." set
			".$column." = '".mysql_real_escape_string($value)."',
			 updatedate = '".$this->Updatedate."' 
			where client_id = '".mysql_real_escape_string($this->Client_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function List_all($limit = null, $offset = null)
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where is_deleted = 0 
			order by title";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function List_all_selectlist()
	{
		$sqlStr = "select client_id, title
			from ".self::DBTable."
			where is_deleted = 0 
			order by title";
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function List_frontend_clients($limit = null, $offset = null)
	{
		$temp_array = array();
	
		$sqlStr = "select client_id, client_md5, title
			from ".self::DBTable."
			where 
			is_deleted = 0 and 
			status = 1 and 
			is_client = 1 
			order by title asc";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		$data_array = $this->__mysql->Get_data_array_assoc($recordset);
		$data_array_count = count($data_array);
		if($data_array_count > 0)
		{
			$i = 0;
			foreach($data_array as $clients)
			{
				$temp_array[$i] = $clients;

				//now load in the campaigns against this client.
				$sqlStr = "select campaign_id, campaign_md5, title, profile_image_thumbnail, url_slug
				from tbl_campaigns
				where 
				client_id = ".$clients['client_id']." and
				is_deleted = 0 and
				status = 1 
				order by updatedate desc
				";
				//echo $sqlStr;
				$recordset = $this->__mysql->Exec_query($sqlStr);
				$camp_array = $this->__mysql->Get_data_array_assoc($recordset);
				$camp_array_count = count($camp_array);
				if($camp_array_count > 0)
				{
					$j = 0;
					foreach($camp_array as $camp)
					{
						$temp_array[$i]['campaigns'][$j] = $camp;
						$j++;
					}
				}
				$i++;
			}
		}
		return $temp_array;
	}
	
	public function Delete($client_id = 0)
	{
		$sqlStr = "update ".self::DBTable." set is_deleted = 1 where client_id = '".mysql_real_escape_string($client_id)."'";
			$this->__mysql->Exec_query($sqlStr);

		$sqlStr = "optimize table ".self::DBTable."";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Select($client_id = 0)
	{
		$sqlStr = "select * from ".self::DBTable." where client_id = '".mysql_real_escape_string($client_id)."'";
		$recordset = $this->__mysql->Exec_query($sqlStr);

		if($this->__mysql->Get_num_rows($recordset) > 0)
		{
			$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Client_id = $result['client_id'];				$this->Client_md5 = $result['client_md5'];				$this->Title = $result['title'];				$this->Logo = $result['logo'];				$this->Client_from = $result['client_from'];				$this->Client_end = $result['client_end'];
				$this->Is_client = $result['is_client'];				$this->Status = $result['status'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
			return true;
		}
		else
		{
			return false;
		}
	}

	public function Select_by($fieldname = null, $value = null)
	{
		if(!is_null($fieldname))
		{
			$sqlStr = "select * from ".self::DBTable." where ".$fieldname." = '".mysql_real_escape_string($value)."'";
			echo $sqlStr."<br />";
			$recordset = $this->__mysql->Exec_query($sqlStr);

			if($this->__mysql->Get_num_rows($recordset) > 0)
			{
				$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Client_id = $result['client_id'];				$this->Client_md5 = $result['client_md5'];				$this->Title = $result['title'];				$this->Logo = $result['logo'];				$this->Client_from = $result['client_from'];				$this->Client_end = $result['client_end'];
				$this->Is_client = $result['is_client'];				$this->Status = $result['status'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}
?>